// 封装uni请求变成Promise风格
import {
	pinia
} from '@/stores/app';

interface IConfig {
	baseUrl : string,
	timeout ?: number,
	header ?: any
}

interface IOptions {
	url : string,
	headers ?: any,
	method ?: "GET" | "POST" | "DELETE" | "PUT",
	data ?: any
}

interface IUploadOptions {
	url : string,
	headers ?: any,
	method ?: "GET" | "POST" | "DELETE" | "PUT",
	data : {
		name : string,
		filePath : any
	}
}

interface IReturnRes {
	data ?: any,
	url ?: string,
	msg : string,
	status : number
}
export default class Request {

	private baseUrl : string;
	private timeout : number;
	private header : any;

	constructor(config : IConfig) {
		this.baseUrl = config.baseUrl;
		this.timeout = config.timeout || 6000;
		this.header = config.header || {};
	}

	_request(options : IOptions) : Promise<IReturnRes> {
		// showLoading
		uni.showLoading({
			title: "",
		});
		return new Promise((resolve, reject) => {
			uni.request({
				url: this.baseUrl + options.url,
				timeout: this.timeout,
				header: {
					// 请求头带token
					"Authori-zation": "Bearer " + pinia.token,
					...this.header,
					...options.headers, // 可以请求时传自定义请求头
				},
				method: options.method || "GET",
				data: options.data,
				success: (res : any) => {
					const data : IReturnRes = res.data;
					uni.hideLoading();
					if (data.status === 400) {
						uni.showToast({
							title: data.msg,
							icon: "none",
							duration: 2000
						});
						// 需要登录权限
					} else if (data.status === 110002) {
						uni.showToast({
							title: data.msg,
							icon: "none"
						});
						pinia.login();
					}
					resolve(data);
				},
				fail: (err : any) => {
					uni.hideLoading();
					uni.showToast({
						title: "请求超时",
						icon: "error",
					});
					reject(err);
				},
				complete: () => {

				},
			});
		});
	}

	get(options : IOptions) : Promise<IReturnRes> {
		return this._request({
			...options,
		});
	}

	post(options : IOptions) : Promise<IReturnRes> {
		return this._request({
			...options,
			method: "POST",
		});
	}

	put(options : IOptions) : Promise<IReturnRes> {
		return this._request({
			...options,
			method: "PUT",
		});
	}

	del(options : IOptions) : Promise<IReturnRes> {
		return this._request({
			...options,
			method: "DELETE",
		});
	}



	// 上传文件
	upload(options : IUploadOptions) : Promise<IReturnRes> {
		uni.showLoading({
			title: "上传中",
		});
		return new Promise((resolve, reject) => {
			uni.uploadFile({
				url: this.baseUrl + options.url,
				header: {
					// 请求头带token
					"Authori-zation": "Bearer " + pinia.token,
					...options.headers,
				},
				filePath: options.data.filePath,
				name: options.data.name,
				formData: {
					filename: options.data.name,
				},
				success: (res : any) => {
					const data : IReturnRes = res.data;
					uni.hideLoading()
					if (data.status === 400) {
						uni.showToast({
							title: data.msg,
							icon: "none",
						});
					} else if (data.status === 110002) {
						uni.showToast({
							title: data.msg,
							icon: "none",
						});
						pinia.login()
					}
					resolve(data);
				},
				fail: (err : any) => {
					uni.hideLoading()
					uni.showToast({
						title: "请求超时",
						icon: "error",
					});
					reject(err);
				},
			});
		});
	}
}